package com.ysu.his.exception;

import com.ysu.his.vo.common.Result;
import com.ysu.his.vo.common.ResultCodeEnum;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE) // 确保最高优先级
public class GlobalExceptionHandler {

    // 1. 优先处理自定义异常
    @ExceptionHandler(HISException.class)
    @ResponseBody
    public Result handleHISException(HISException e) {

        System.out.println(e.getResultCodeEnum().getCode());
        return Result.fail(e.getResultCodeEnum());
    }

    // 2. 兜底异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Result> handleException(Exception e) {
        System.out.println(e);
        return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(Result.fail(ResultCodeEnum.SYSTEM_ERROR));
    }
}
